package com.qingxin.wuye.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyRepairOrders;

@Service
public class WyRepairOrdersService extends BaseService<WyRepairOrders, String> {

	public Pager list(String companyid, String gardenid, String roomCode, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.entity.WyRepairOrders(" +
				"r.ordersid, r.serialNumber, r.repairTime, r.repairType, r.contents, r.roomCode, r.dispatchStatus," +
				" r.completionStatus, r.returnVisitsStatus, r.linkman, r.linkphone, r.appointmentTime, r.maintainKind," +
				" r.customKind, r.secondaryMaintain, r.remark, r.cruser, r.crtime, g.gardenName)" +
				" from WyRepairOrders r, WyGarden g" +
				" where r.gardenid=g.gardenid ");
		
		if(StringUtils.isNotEmpty(companyid) && !"0".equals(companyid)){
			querier.append(" and g.wyPropertyCompany.companyid=:companyid ");
			querier.setParam("companyid", companyid);
		}
		
		if(StringUtils.isNotEmpty(gardenid) && !"0".equals(gardenid)){
			querier.append(" and r.gardenid=:gardenid ");
			querier.setParam("gardenid", gardenid);
		}
		
		if(StringUtils.isNotEmpty(roomCode)){
			roomCode = roomCode.replace("'", "");
			querier.append(" and r.roomCode like :roomCode ");
			querier.setParam("roomCode", "%"+roomCode+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by r.createTime desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加信息
	 * @param orders
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WyRepairOrders orders, LoginUser loginUser, HttpServletRequest request){
		
		Date now = new Date();
		orders.setCrtime(now);
		orders.setCruser(loginUser.getLogin_name());
		
		this.save(orders);
		
		return null;
	}
	
	
	/**
	 * 删除信息
	 * @param ordersids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] ordersids, LoginUser loginUser, HttpServletRequest request){
		for(String ordersid : ordersids){
			WyRepairOrders orders = get(ordersid);
			if(orders==null){
				continue;
			}
			
			this.delete(orders);
		}
		return null;
	}
	
}
